import 'package:bjmanagement/layout/layout.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_treeview/tree_view.dart' as tv;
import 'package:get/get.dart';

// 菜单测试
var _treeMenu = [
  tv.Node(
    label: 'Documents',
    key: 'docs',
    expanded: true,
    icon: tv.NodeIcon(
      codePoint: Icons.folder.codePoint,
      color: "blue",
    ),
    children: [
      tv.Node(
          label: 'Job Search',
          key: 'd3',
          icon: tv.NodeIcon.fromIconData(Icons.input),
          children: [
            tv.Node(
                label: 'Resume.docx',
                key: 'pd1',
                icon: tv.NodeIcon.fromIconData(Icons.insert_drive_file)),
            tv.Node(
                label: 'Cover Letter.docx',
                key: 'pd2',
                icon: tv.NodeIcon.fromIconData(Icons.insert_drive_file)),
          ]),
      tv.Node(
        label: 'Inspection.docx',
        key: 'd1',
      ),
      tv.Node(
          label: 'Invoice.docx',
          key: 'd2',
          icon: tv.NodeIcon.fromIconData(Icons.insert_drive_file)),
    ],
  ),
  tv.Node(
      label: 'MeetingReport.xls',
      key: 'mrxls',
      icon: tv.NodeIcon.fromIconData(Icons.insert_drive_file)),
  tv.Node(
      label: 'MeetingReport.pdf',
      key: 'mrpdf',
      icon: tv.NodeIcon.fromIconData(Icons.insert_drive_file)),
  tv.Node(
      label: 'Demo.zip',
      key: 'demo',
      icon: tv.NodeIcon.fromIconData(Icons.archive)),
];

// 树型结构控件
class TView extends StatelessWidget {
  final List<tv.Node> nodes;

  TView({Key key, @required this.nodes}) : super(key: key) {
    Get.put(TViewController(nodes));
  }

  @override
  Widget build(BuildContext context) => GetBuilder(builder: _getBuild);

  // 构建界面
  Widget _getBuild(TViewController _ctl) {
    return tv.TreeView(
      controller: _ctl._treeViewController,
      allowParentSelect: false,
      supportParentDoubleTap: false,
      onExpansionChanged: _ctl._expandNode,
      onNodeTap: (key) {
        tv.Node node = _ctl._treeViewController.getNode(key);
        if (node != null) {
          LayoutController ctl = Get.find();
          ctl.addPageView(
            LayoutView(
              id: key,
              name: node.label,
            ),
          );
          _ctl.selectKey(key);
        }
      },
      theme: _ctl._treeViewTheme,
    );
  }
}

// treeview控制器
class TViewController extends GetxController {
  tv.TreeViewController _treeViewController;

  tv.TreeViewTheme _treeViewTheme = tv.TreeViewTheme(
    expanderTheme: tv.ExpanderThemeData(
        type: tv.ExpanderType.caret,
        modifier: tv.ExpanderModifier.none,
        position: tv.ExpanderPosition.start,
        // color: Colors.grey.shade800,
        size: 20,
        color: Colors.blue),
    labelStyle: TextStyle(
      fontSize: 16,
      letterSpacing: 0.3,
    ),
    parentLabelStyle: TextStyle(
      fontSize: 16,
      letterSpacing: 0.1,
      fontWeight: FontWeight.w800,
      color: Colors.blue.shade700,
    ),
    iconTheme: IconThemeData(
      size: 18,
      color: Colors.grey.shade800,
    ),
    // colorScheme: Get,
  );

  TViewController(List<tv.Node> nodes) {
    _treeViewController = tv.TreeViewController(children: nodes);
  }

  updateListNode(List<tv.Node> nodes) {
    _treeViewController = tv.TreeViewController(children: nodes);
    update();
  }

  updateTreeViewTheme(tv.TreeViewTheme theme) {
    _treeViewTheme = theme;
    update();
  }

  selectKey(String key) {
    tv.Node node = _treeViewController.getNode(key);
    if (node != null) {
      _treeViewController = _treeViewController.copyWith(selectedKey: key);
    }
    update();
  }

  _expandNode(String key, bool expanded) {
    tv.Node node = _treeViewController.getNode(key);
    if (node != null) {
      List<tv.Node> updated;
      updated = _treeViewController.updateNode(
        key,
        node.copyWith(
            expanded: expanded,
            icon: tv.NodeIcon(
              codePoint: expanded
                  ? Icons.folder_open.codePoint
                  : Icons.folder.codePoint,
              color: expanded ? "blue600" : "grey700",
            )),
      );
      _treeViewController = _treeViewController.copyWith(children: updated);
      update();
    }
  }
}
